package com.gopay.bis.manager.bank.payconfig.impl;

import java.util.Date;

import javax.annotation.Resource;

import com.gopay.common.cipher.utils.LoggerMaskUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.gopay.bis.dao.payconfig.BisExpaySmsSendLogDAO;
import com.gopay.bis.manager.bank.payconfig.SmsSupportService;
import com.gopay.common.constants.bank.SmsSendSourceConstants;
import com.gopay.common.constants.cust.SMSType;
import com.gopay.common.domain.bank.ExPayBaseMessage;
import com.gopay.common.domain.log.BisExpaySmsSendLog;
import com.gopay.sms.client.GopaySMSHttpClient;
import com.gopay.sms.common.constants.SMSAppId;
import com.gopay.sms.common.constants.SMSResCode;
import com.gopay.sms.common.domain.RequestVO;
import com.gopay.sms.common.domain.ResponseVO;

/**
 * Created by Administrator on 14-8-21.
 */
@Service("smsSupportService")
public class SmsSupportServiceImpl implements SmsSupportService {
    
    @Resource(name = "bisExpaySmsSendLogDAO")
    private BisExpaySmsSendLogDAO bisExpaySmsSendLogDAO;

    private static Logger log = Logger.getLogger(SmsSupportServiceImpl.class);
    
    public boolean sendSMS(ExPayBaseMessage baseMsg) {
        RequestVO req = new RequestVO();
        req.setMobile(baseMsg.getMediaId());
        req.setAppId(SMSAppId._90001.value);
        req.setContent(baseMsg.getSmsContent());
        ResponseVO resposeVo = GopaySMSHttpClient.getInstance().createVerificationCode(req);
        boolean res =  resposeVo!=null && resposeVo.getStatusCode().equals(SMSResCode.SMS_DEND_SUCCESS.value);
        log.info("send sms mobileNo:"+ LoggerMaskUtils.maskString(baseMsg.getMediaId())+";result:"+res+";orderId:"+baseMsg.getOrderId());
        if(res){
            //发送成功，记录发送成功次数(按orderId保存)
            buildSendLog(baseMsg, SMSType.CASHIER_SMS_SEND);
        }
        return res;
    }


    public boolean checkSMS(ExPayBaseMessage baseMsg){
        RequestVO req = new RequestVO();
        req.setAppId(SMSAppId._90001.value);
        req.setMobile(baseMsg.getMediaId());
        req.setCode(baseMsg.getValidateCode());
        req.setTimeout(baseMsg.getTimeOut());//验证码有效期
        ResponseVO resposeVo = GopaySMSHttpClient.getInstance().verifyCode(req);
        boolean res =  resposeVo !=null && StringUtils.equals(resposeVo.getStatusCode(), SMSResCode.SMS_VERIFY_SUCCESS.value);
        log.info("check sms mobileNo:"+LoggerMaskUtils.maskString(baseMsg.getMediaId())+";result:"+res+";orderId:"+baseMsg.getOrderId());
        if(!res){
            //验证错误，记录错误次数(按orderId保存)
            buildCheckLog(baseMsg, SMSType.CASHIER_SMS_ERROR);
        }
        return res;
        
    }

    public boolean checkSendSmsCount(String orderId,int maxCount){
    	int count = bisExpaySmsSendLogDAO.findToDayCountByOrderId(orderId,  SMSType.CASHIER_SMS_SEND.value);
        log.info("check sms send count---orderId:"+orderId+",count:"+count);
        return !(count > maxCount-1);
    }
    
    /**
     * 根据订单号获取发送次数
     */
	public int checkSendSmsCont(String orderId) {
	    int count = bisExpaySmsSendLogDAO.findToDayCountByOrderId(orderId, SMSType.CASHIER_SMS_SEND.value);
        log.info("check sms send count---orderId:"+orderId+",count:"+count);
        return count;
	}

	/**
	 * 根据订单号查询错误次数
	 */
    public int checkFailSmsCount(String orderId){
        int count = bisExpaySmsSendLogDAO.findToDayCountByOrderId(orderId, SMSType.CASHIER_SMS_ERROR.value);
        log.info("check sms fail count---orderId:"+orderId+",count:"+count);
        return count;
    }
    
    /**
     * 根据订单号，卡号，查询错误次数
     */
    public int checkCardFailSmsCount(String orderId,String cardNO) {
        int count = bisExpaySmsSendLogDAO.findToDayCountByOrderIdCardNO(orderId, cardNO, SMSType.CASHIER_SMS_ERROR.value);
        log.info("check sms fail count---order:"+orderId+",cardNo:"+LoggerMaskUtils.maskString(cardNO)+",count:"+count);
        return count;
    }
    
    
    /**
     * 发送和校验的手机号是否匹配
     */
    public boolean checkSendMobile(ExPayBaseMessage baseMsg) {
        BisExpaySmsSendLog log = bisExpaySmsSendLogDAO.getBisExpaySmsSendLog(baseMsg.getOrderId(), SMSType.CASHIER_SMS_SEND.value);
        if(log != null && (baseMsg.getMediaId()).equals(log.getMobileNum())){
            return true;
        }
        //验证错误，记录错误次数(按orderId保存)
        buildCheckLog(baseMsg, SMSType.CASHIER_SMS_ERROR);
        return false;
    }


    /** 
     * 发送验证码成功后，插入一条发送成功日志
     * @param baseMsg
     * @param smsType 
     */
    private void buildSendLog(ExPayBaseMessage baseMsg,SMSType smsType){
        BisExpaySmsSendLog log = new BisExpaySmsSendLog();
        Date date = new Date();
        log.setMobileNum(baseMsg.getMediaId());
        log.setModuleId(smsType.value);
        log.setOrderId(baseMsg.getOrderId());
        log.setCardNo(baseMsg.getCardNo());
        log.setOperateSource(SmsSendSourceConstants.SEND_SOURCE_GFB);
        log.setCreatedTime(date);
        bisExpaySmsSendLogDAO.save(log);
        
    }
    /** 
     * 验证码检验不通过，插入一条校验不通过的日志
     * @param baseMsg
     * @param smsType 
     */
    private void buildCheckLog(ExPayBaseMessage baseMsg,SMSType smsType){
        BisExpaySmsSendLog log = new BisExpaySmsSendLog();
        Date date = new Date();
        log.setMobileNum(baseMsg.getMediaId());
        log.setModuleId(smsType.value);
        log.setOrderId(baseMsg.getOrderId());
        log.setCardNo(baseMsg.getCardNo());
        log.setOperateSource(SmsSendSourceConstants.CHECK_SOURCE_GFB);
        log.setCreatedTime(date);
        bisExpaySmsSendLogDAO.save(log);
    }





 
  


   
}
